package com.dtda.edu.miaosha.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.dtda.edu.miaosha.bo.UserInfoBo;
import com.dtda.edu.miaosha.common.exception.BaseExcpController;
import com.dtda.edu.miaosha.service.UserService;

/**
 * @author 袁强
 * @version 2019年1月14日 下午9:19:50
 * 类说明  
 */
@RestController
@RequestMapping("/")
public class LoginController extends BaseExcpController{

	@Autowired
	private UserService userService;
	@RequestMapping("/doLogin")
	public ModelAndView doLogin() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/user/login");
		return modelAndView;
	}
	@RequestMapping("/login")
	public ModelAndView login(HttpServletRequest request,
							  @RequestParam(name="telphone") String telphone,
			                  @RequestParam(name="encrpt_password") String encrpt_password) {
		ModelAndView modelAndView = new ModelAndView();
		UserInfoBo userInfoBo = userService.selectByTelandPass(telphone,encrpt_password);
		if(userInfoBo == null) {
			modelAndView.setViewName("/user/login");  //登录失败,重新登录
			return modelAndView;
		}
		/*
		 * 在创建订单的controller层中使用过
		 */
		request.getSession().setAttribute("IS_LOGIN","true");
		request.getSession().setAttribute("LOGIN_USER",userInfoBo);
		modelAndView.addObject("userName",userInfoBo);
		modelAndView.setViewName("/project/showItems");
		return modelAndView;
	}
}
